package com.tanchengjin.admin.modules.blog.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

/**
 * java-jwt工具类
 */
public class JWTUtil {
    /**
     * 过期时间(毫秒)
     */
    private static final long EXPIRE_TIME = (60 * 60) * 60 * 60;

    /**
     * jwt签名
     *
     * @param secret   密钥
     * @param username 用户名
     * @return token
     */
    public static String sign(String secret, String username) {

        Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        Algorithm algorithm = getAlgorithm(secret);
        return JWT.create()
                .withClaim("username", "username")
                .withExpiresAt(expireAt)
                .sign(algorithm);
    }

    public static String sign(String secret, String username, long uid) {
        Date expireAt = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        return sign(secret, username, uid, expireAt);
    }

    public static String sign(String secret, String username, long uid, Date expireAt) {
        Algorithm algorithm = getAlgorithm(secret);
        return JWT.create()
                .withClaim("username", "username")
                .withClaim("uid", uid)
                .withExpiresAt(expireAt)
                .sign(algorithm);
    }

    /**
     * 获取refresh_token
     */
    public static String get_refresh_token(String secret, String username, long uid, Date expireAt) {
        return sign(secret, username, uid, expireAt);
    }

    /**
     * JWT算法
     *
     * @param secret 加密字符串
     * @return Algorithm
     */
    private static Algorithm getAlgorithm(String secret) {
        return Algorithm.HMAC256(secret);
    }


    /**
     * 校验token是否国企
     *
     * @param token token
     * @return true or false
     */
    public static boolean isExpired(String token) {
        return JWT.decode(token).getExpiresAt().before(new Date());
    }

    /**
     * 校验token是否正确
     *
     * @param token token
     * @return 是否校验成功
     */
    public static boolean verify(String token) {
        Algorithm secret = Algorithm.HMAC256("secret");
        JWTVerifier build = JWT.require(secret).withClaim("username", "username").build();
        DecodedJWT verify = build.verify(token);
        return true;
    }

    public static String getUID(String token) {
        DecodedJWT decode = JWT.decode(token);
        Claim uid = decode.getClaim("uid");
        return uid.asString();
    }

    /**
     * 获取token过期时间
     *
     * @param token token
     * @return 过期时间
     */
    public static Date getExpiresAt(String token) {
        DecodedJWT decode = JWT.decode(token);
        Date expiresAt = decode.getExpiresAt();
        Claim username = decode.getClaim("username");
        System.out.println(username);
        return expiresAt;
    }

    public static void main(String[] args) {
//        String token = JWTUtil.sign("123123123", "3333");
//        System.out.println(token);
        boolean verify = JWTUtil.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2Mzc5MjMxNzgsInVzZXJuYW1lIjoidXNlcm5hbWUifQ.wnE-59QMDsRCs9nQcIrlxt_LUPFUDu690zDHnk6aBAc");
        System.out.println(verify);


    }


}
